#include "Precompiled.h"
#include "EntityLogicManager.h"
#include "RTTI.h"

namespace Game {
namespace Common {

EntityLogicManager::EntityLogicManager() {
}

EntityLogicManager::~EntityLogicManager() {
}

IEntityLogic* EntityLogicManager::GetEntityLogic(const RTTI& rtti) const {
    EntityLogicMap::const_iterator it = this->entityLogicMap.find(rtti.GetClassName());
    if (it != this->entityLogicMap.end()) {
        return (*it).second;
    }

    return NULL;
}

void EntityLogicManager::Free() {
    EntityLogicMap::const_iterator it = this->entityLogicMap.begin();
    EntityLogicMap::const_iterator end = this->entityLogicMap.end();
    for (; it != end; ++it) {
        IEntityLogic* gameLogic = it->second;
        SAFE_DELETE(gameLogic);
    }

    this->entityLogicMap.clear();
}

void EntityLogicManager::Register(IEntityLogic* gameLogic) {
    this->entityLogicMap.insert(EntityLogicMapPair(gameLogic->GetEntityRTTI().GetClassName(), gameLogic));
    gameLogic->SetEntityLogicManager(this);
}

}
}